<html>
<head><meta charset="utf-8"><title>Lifetime of value · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html">Lifetime of value</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="218775636"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218775636" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> CraftSpider <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218775636">(Dec 04 2020 at 00:50)</a>:</h4>
<p>Is there any syntax, or has there ever been a proposal, for syntax that would allow something like this:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">impl</span><span class="o">&lt;'</span><span class="na">a</span><span class="p">,</span><span class="w"> </span><span class="n">T</span>: <span class="o">'</span><span class="na">a</span><span class="o">&gt;</span><span class="w"> </span><span class="n">Trait</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">T</span><span class="p">,)</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="o">'</span><span class="na">a</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="o">..</span><span class="p">.</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>The basic idea I'm trying to convey is 'valid for all tuples that contain types that won't outlive the tuple'. So <code>(&amp;i32)</code> would fit the impl only for cases where <code>&amp;'b (&amp;'a i32)</code> fits <code>'b: 'a</code></p>



<a name="218775770"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218775770" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> CraftSpider <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218775770">(Dec 04 2020 at 00:52)</a>:</h4>
<p>Maybe a more clear syntax would be</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">impl</span><span class="o">&lt;'</span><span class="na">a</span><span class="p">,</span><span class="w"> </span><span class="o">'</span><span class="na">b</span>: <span class="o">'</span><span class="na">a</span><span class="p">,</span><span class="w"> </span><span class="n">T</span>: <span class="o">'</span><span class="na">a</span><span class="o">&gt;</span><span class="w"> </span><span class="n">Trait</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">T</span><span class="p">,)</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="o">'</span><span class="na">b</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="o">..</span><span class="p">.</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="218776765"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218776765" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Quy Nguyen <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218776765">(Dec 04 2020 at 01:08)</a>:</h4>
<p>What's the use case? You can get a shorter lifetime from a longer one via a reborrow.</p>



<a name="218777929"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218777929" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> CraftSpider <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218777929">(Dec 04 2020 at 01:30)</a>:</h4>
<p>The current code is, trimmed down, roughly:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">type</span> <span class="nc">HelperFn</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Box</span><span class="o">&lt;</span><span class="k">dyn</span><span class="w"> </span><span class="k">for</span><span class="o">&lt;'</span><span class="na">a</span><span class="o">&gt;</span><span class="w"> </span><span class="nb">Fn</span><span class="p">(</span><span class="o">&amp;</span><span class="n">Value</span><span class="o">&lt;'</span><span class="na">a</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">Value</span><span class="o">&lt;'</span><span class="na">a</span><span class="o">&gt;</span><span class="p">;</span><span class="w"></span>

<span class="k">impl</span><span class="o">&lt;</span><span class="n">T0</span>: <span class="nc">Reflected</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="o">'</span><span class="nb">static</span><span class="o">&gt;</span><span class="w"> </span><span class="n">Trait</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">T0</span><span class="p">,)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">helpers</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="nc">HelperFn</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="nb">Box</span>::<span class="n">new</span><span class="p">(</span><span class="o">|</span><span class="n">this</span><span class="o">|</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="kd">let</span><span class="w"> </span><span class="n">inner</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">this</span><span class="p">.</span><span class="n">borrow</span>::<span class="o">&lt;</span><span class="p">(</span><span class="n">T0</span><span class="p">,)</span><span class="o">&gt;</span><span class="p">();</span><span class="w"></span>
<span class="w">            </span><span class="n">Value</span>::<span class="n">from_ref</span><span class="p">(</span><span class="o">&amp;</span><span class="n">inner</span><span class="p">.</span><span class="mi">0</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="p">})</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>The <code>'static</code> bound makes it work as-is, but this should theoretically be sound for non-static <code>T0</code> lifetimes as long as the type being implemented (in this case, a tuple) outlives them. It's acceptable for my purposes, but I was looking if there had ever been proposals or discussion around the pros/cons of allowing this.</p>



<a name="218797147"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218797147" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218797147">(Dec 04 2020 at 06:02)</a>:</h4>
<p>It's not clear to me that this is actually an expressivity limitation. Do you have an example that doesn't compile without the fancy bound?</p>



<a name="218798447"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218798447" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218798447">(Dec 04 2020 at 06:37)</a>:</h4>
<p><span class="user-mention" data-user-id="319144">@Rune Tynan</span> Do you have a playground link for that? What are the signatures of <code>Value::borrow</code> and <code>Value::from_ref</code>?</p>



<a name="218824837"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218824837" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> CraftSpider <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218824837">(Dec 04 2020 at 12:13)</a>:</h4>
<p><code>Value&lt;'a&gt;::borrow</code> is <code>fn borrow(&amp;self) -&gt; &amp;'a T</code>, <code>Value::from_ref</code> is <code>fn from_ref&lt;'a, T&gt;(&amp;'a T) -&gt; Value&lt;'a&gt;</code>. The code won't compile without the bound as is, the reason being a case like this from what I can tell:<br>
given <code>&amp;'a (&amp;'b i32)</code> you can't return a <code>&amp;'a &amp;'b i32</code> if <code>'a</code> outlives <code>'b</code>, which can't be proven in the general case, and there seems to be no syntax to add such bounding of the inner type to the lifetime of the outer.</p>



<a name="218825793"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218825793" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> CraftSpider <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218825793">(Dec 04 2020 at 12:24)</a>:</h4>
<p>Playground Example: <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=429714bfb40de5056e29290be8892910">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=429714bfb40de5056e29290be8892910</a></p>



<a name="218825839"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218825839" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218825839">(Dec 04 2020 at 12:25)</a>:</h4>
<p>I think that <code>WF(&amp;'a (&amp;'b i32,))</code> implies <code>'b: 'a</code>, but you should check with the chalk / wg-traits folks to be sure</p>



<a name="218825994"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218825994" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218825994">(Dec 04 2020 at 12:27)</a>:</h4>
<p>although that makes <code>borrow</code> a bit weird, because it's manufacturing a <code>T</code> from nothing</p>



<a name="218826286"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218826286" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> CraftSpider <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218826286">(Dec 04 2020 at 12:30)</a>:</h4>
<p>Hm, alright. Though if that's a bad example, doesn't help that the compiler still thinks there exists <em>some</em> refutation to the soundness of the closure. And I stripped out in the playground a lot of the safety code, the real implementation of <code>Value</code> basically stores what <code>T</code> is and compares the <code>T</code> passed to <code>borrow</code> to what it knows the real value is.</p>



<a name="218826310"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218826310" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218826310">(Dec 04 2020 at 12:31)</a>:</h4>
<p>How can you even put <code>T0: 'a</code> in <code>helpers()</code>? The lifetime in question is being quantified over in the <code>dyn fn</code> constructed in the box</p>



<a name="218826338"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218826338" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218826338">(Dec 04 2020 at 12:31)</a>:</h4>
<p>what is <code>'a</code> supposed to refer to?</p>



<a name="218826446"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218826446" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> CraftSpider <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218826446">(Dec 04 2020 at 12:32)</a>:</h4>
<p>The existential used for the <code>dyn for&lt;'a&gt; Fn</code> is using higher-kinded types, it says 'for all <code>'a</code> this function will be valid', so the error is that the compiler thinks there exists <em>some</em> <code>T0</code> such that the closure generated will not be valid.</p>



<a name="218826452"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218826452" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218826452">(Dec 04 2020 at 12:32)</a>:</h4>
<p><span class="user-mention silent" data-user-id="319144">Rune Tynan</span> <a href="#narrow/stream/122651-general/topic/Lifetime.20of.20value/near/218826286">said</a>:</p>
<blockquote>
<p>And I stripped out in the playground a lot of the safety code, the real implementation of <code>Value</code> basically stores what <code>T</code> is and compares the <code>T</code> passed to <code>borrow</code> to what it knows the real value is.</p>
</blockquote>
<p>I don't think that your safety code can check lifetimes, so you have to assume that <code>T: 'static</code> in <code>borrow</code></p>



<a name="218826500"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218826500" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218826500">(Dec 04 2020 at 12:33)</a>:</h4>
<p>right, no bound other than <code>static</code> will work in <code>helpers</code></p>



<a name="218826628"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218826628" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218826628">(Dec 04 2020 at 12:34)</a>:</h4>
<p>my impression is that you think there is a valid way to express this, possibly including features not in today's rust, but I don't yet see what the correct version of <code>helpers</code> is supposed to look like</p>



<a name="218826645"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218826645" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> CraftSpider <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218826645">(Dec 04 2020 at 12:34)</a>:</h4>
<p>Yeah. Thus my original question of 'has there ever been a proposal that would allow expressing the real bound, because the code would work for non-static as long as it obeyed certain other rules'.</p>



<a name="218826677"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218826677" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218826677">(Dec 04 2020 at 12:35)</a>:</h4>
<p>What does the code look like with the "real bound"?</p>



<a name="218826713"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218826713" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218826713">(Dec 04 2020 at 12:35)</a>:</h4>
<p>because just putting <code>T0</code> there or <code>T0: 'a</code> obviously doesn't work</p>



<a name="218826796"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218826796" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> CraftSpider <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218826796">(Dec 04 2020 at 12:36)</a>:</h4>
<p>That was my original code, attempting to express that case of all types that live long enough that the reference to the thing inside the tuple cannot outlive the reference to the tuple</p>



<a name="218826915"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218826915" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218826915">(Dec 04 2020 at 12:38)</a>:</h4>
<p>If I understand you correctly, that means that <code>borrow</code> has to return a non-static T and I don't think that can ever be correct</p>



<a name="218826969"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218826969" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> CraftSpider <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218826969">(Dec 04 2020 at 12:38)</a>:</h4>
<p>And I think <code>borrow</code> works for non-static types because of your earlier WF, <code>&amp;'a T&lt;'b&gt;</code> implies <code>'b: 'a</code>, and <code>Value</code> cannot outlive <code>'a</code></p>



<a name="218827003"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218827003" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218827003">(Dec 04 2020 at 12:39)</a>:</h4>
<p>But the input to <code>borrow</code> doesn't have any <code>'b</code> in it</p>



<a name="218827012"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218827012" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218827012">(Dec 04 2020 at 12:39)</a>:</h4>
<p>it's magicking up a <code>T</code> from nowhere</p>



<a name="218827026"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218827026" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218827026">(Dec 04 2020 at 12:39)</a>:</h4>
<p>so this has to be an unsafe function if <code>T</code> is not static</p>



<a name="218827052"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218827052" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218827052">(Dec 04 2020 at 12:39)</a>:</h4>
<p>because no safety checks can check the lifetimes because lifetimes are not runtime observable</p>



<a name="218827181"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218827181" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> CraftSpider <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218827181">(Dec 04 2020 at 12:41)</a>:</h4>
<p>Ah, true. <code>Value</code> can ensure it lives long enough, but the lifetime of the output will be 'whatever it needs to be'</p>



<a name="218827340"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218827340" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> CraftSpider <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218827340">(Dec 04 2020 at 12:42)</a>:</h4>
<p>Actually, hm. Is that true? The reference returned is <code>'a</code>, which definitely outlives <code>'b</code>, so it should never make <code>'b</code> greater than <code>'a</code>, as that would break the same check</p>



<a name="218827395"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218827395" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218827395">(Dec 04 2020 at 12:43)</a>:</h4>
<p>is that <code>dyn fn</code> polymorphic over two lifetimes or just one? I see a <code>&amp;</code> in there</p>



<a name="218827459"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218827459" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> CraftSpider <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218827459">(Dec 04 2020 at 12:43)</a>:</h4>
<p>Two, though from the error messages I've gotten playing around, the compiler ensures it's valid for all <code>&amp;'r</code> as well</p>



<a name="218827543"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218827543" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218827543">(Dec 04 2020 at 12:44)</a>:</h4>
<p>I think it depends on whether <code>Container</code> is covariant</p>



<a name="218827575"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218827575" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> CraftSpider <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218827575">(Dec 04 2020 at 12:44)</a>:</h4>
<p>When you constrain <code>T0</code> in bad ways, the error becomes <code>Expected dyn for&lt;'r, 'a&gt; Fn(&amp;'r Value&lt;'a&gt;) -&gt; Value&lt;'a&gt; got [something less general]</code></p>



<a name="218827625"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218827625" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218827625">(Dec 04 2020 at 12:45)</a>:</h4>
<p>yeah <code>'a: 'r</code> is implicit in that bound btw</p>



<a name="218827746"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218827746" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> CraftSpider <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218827746">(Dec 04 2020 at 12:46)</a>:</h4>
<p>Yeah, that's fine. In fact, that's I presume why it still works when I changed something that uses the Fn box to have a signature of <code>(&amp;'a Value&lt;'a&gt;) -&gt; Value&lt;'a&gt;</code>, because that bound was already implicit</p>



<a name="218827814"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218827814" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218827814">(Dec 04 2020 at 12:47)</a>:</h4>
<p>Yeah those are equivalent if <code>Value</code> is covariant, and your phantom data will do so</p>



<a name="218827942"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218827942" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218827942">(Dec 04 2020 at 12:48)</a>:</h4>
<p>wait a minute, <code>from_ref</code> also looks shady. It captures its argument with a smaller lifetime than <code>'a</code>, what prevents it from expiring?</p>



<a name="218828161"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218828161" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> CraftSpider <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218828161">(Dec 04 2020 at 12:51)</a>:</h4>
<p>So, your question is 'Is <code>from_ref</code> capable of capturing a reference to a thing that outlives the thing'? Because I thought that was part of the compiler's WF guarantee</p>



<a name="218828203"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218828203" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218828203">(Dec 04 2020 at 12:51)</a>:</h4>
<p>er, actually I guess that's <code>&amp;'b T -&gt; Container&lt;'b&gt;</code> because of lifetime elision?</p>



<a name="218828214"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218828214" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> CraftSpider <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218828214">(Dec 04 2020 at 12:51)</a>:</h4>
<p>Yeah</p>



<a name="218828270"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218828270" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218828270">(Dec 04 2020 at 12:52)</a>:</h4>
<p>which is basically the same as <code>&amp;'a T -&gt; Container&lt;'a&gt;</code> even though <code>'a</code> is already in scope</p>



<a name="218828274"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218828274" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> CraftSpider <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218828274">(Dec 04 2020 at 12:52)</a>:</h4>
<p>Yeah</p>



<a name="218828286"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218828286" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218828286">(Dec 04 2020 at 12:52)</a>:</h4>
<p>so your function has two lifetime parameters and one is unused. <span aria-label="shrug" class="emoji emoji-1f937" role="img" title="shrug">:shrug:</span></p>



<a name="218828337"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218828337" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> CraftSpider <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218828337">(Dec 04 2020 at 12:53)</a>:</h4>
<p>The inner <code>T</code> may have a lifetime, but the reference will never outlive it, so the compiler finds it sound. At least that's what I assume, as if that wasn't true I'd expect to get the same 'T doesn't live long enough' error</p>



<a name="218828354"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218828354" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> CraftSpider <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218828354">(Dec 04 2020 at 12:53)</a>:</h4>
<p>I'm gonna need to head out soon, but thanks for talking through all this. The nitty-gritty of lifetimes is really interesting, and this project is really diving into it. I'll see if I can find anything on the whole 'given input <code>&amp;'a T&lt;'b&gt;</code> where <code>'b: 'a</code>, and later output <code>&amp;'a T</code>, is the compiler guaranteed to pick a new <code>'b</code> so that <code>'a: 'b</code> and the original lifetimes aren't violated'</p>



<a name="218828503"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218828503" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> CraftSpider <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218828503">(Dec 04 2020 at 12:55)</a>:</h4>
<p>As if there was no <code>'a</code> in the Value, I'm sure that would be unsafe. But as there is, and <code>'a</code> can't outlive the object, I think it should work. Actually, I can test that. Make a <code>Value</code> and see if I can <code>borrow</code> something out that outlives it.</p>



<a name="218828545"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218828545" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218828545">(Dec 04 2020 at 12:55)</a>:</h4>
<p>Okay here's a version with all the lifetimes written out so I can point at it: <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=9d97cac0d2679d53b936d014e15a9d53">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=9d97cac0d2679d53b936d014e15a9d53</a></p>



<a name="218828714"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218828714" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218828714">(Dec 04 2020 at 12:57)</a>:</h4>
<p>So because the <code>dyn fn</code> quantifies over <code>'a</code>, it is not possible to add a bound <code>'b: 'a</code>, but this is implied by the WF bound when constructing <code>&amp;'a (T0,)</code></p>



<a name="218828962"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218828962" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218828962">(Dec 04 2020 at 12:59)</a>:</h4>
<p>If you actually wanted to support a non-static <code>T0</code> here, you would need something like</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">type</span> <span class="nc">HelperFn</span><span class="o">&lt;'</span><span class="na">b</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Box</span><span class="o">&lt;</span><span class="k">dyn</span><span class="w"> </span><span class="k">for</span><span class="o">&lt;'</span><span class="na">a</span>: <span class="o">'</span><span class="na">b</span><span class="o">&gt;</span><span class="w"> </span><span class="nb">Fn</span><span class="p">(</span><span class="o">&amp;'</span><span class="na">a</span><span class="w"> </span><span class="n">Container</span><span class="o">&lt;'</span><span class="na">a</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">Container</span><span class="o">&lt;'</span><span class="na">a</span><span class="o">&gt;&gt;</span><span class="p">;</span><span class="w"></span>
</code></pre></div>
<p>although I'm not sure that it is legal to put lifetime bounds in function signatures like that</p>



<a name="218829050"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218829050" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> CraftSpider <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218829050">(Dec 04 2020 at 13:00)</a>:</h4>
<p><code>for&lt;...&gt;</code> doesn't allow lifetime constraints, I tried some</p>



<a name="218829098"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218829098" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> CraftSpider <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218829098">(Dec 04 2020 at 13:00)</a>:</h4>
<p>Might be theoretically possible for it to, might not be. Probably have to go read up the higher-kinded-types stuff</p>



<a name="218829105"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218829105" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218829105">(Dec 04 2020 at 13:00)</a>:</h4>
<p>It's logical given the theory, but I think there are some inference issues. I know it's on their radar</p>



<a name="218863327"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218863327" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> CraftSpider <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218863327">(Dec 04 2020 at 17:21)</a>:</h4>
<p>I tried it out, to show Value::from_ref (and one I didn't show, Value::from) won't borrow past lifetime:</p>
<div class="codehilite"><pre><span></span><code>error[E0597]: `i` does not live long enough
   --&gt; src\lib.rs:204:33
    |
204 |             v = Value::from_ref(&amp;i);
    |                                 ^^ borrowed value does not live long enough
...
211 |         }
    |         - `i` dropped here while still borrowed
212 |
213 |         let t = v.borrow::&lt;i32&gt;();
    |                 - borrow later used here
</code></pre></div>
<div class="codehilite"><pre><span></span><code>error[E0597]: `i` does not live long enough
   --&gt; src\lib.rs:185:27
    |
185 |             t = Test { r: &amp;i };
    |                           ^^ borrowed value does not live long enough
...
193 |         }
    |         - `i` dropped here while still borrowed
194 |
195 |         let t = v.borrow::&lt;Test&gt;();
    |                 - borrow later used here
</code></pre></div>



<a name="218863556"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218863556" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> CraftSpider <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218863556">(Dec 04 2020 at 17:23)</a>:</h4>
<p>These cases work if I comment out any code that tries to use the value beyond the borrowed reference, yay for the borrow checker :P</p>



<a name="218869297"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218869297" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> CraftSpider <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218869297">(Dec 04 2020 at 18:01)</a>:</h4>
<p>Just fixed some unsoundness with a family of <code>borrow</code>, <code>cast</code>. Because <code>cast</code> takes ownership, what it returns <em>can</em> outlive the Value, and thus <em>can</em> end up magicking up a <code>'static</code> lifetime. The solution is A) Make it only valid when casting to types with a <code>'static</code> bound B) Make it only valid when Value 'owns' its internal data. First is compile checked, second causes a runtime error. That way you can only magic up a lifetime if you own the type, and thus have the authority to do so.</p>



<a name="218870130"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218870130" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> CraftSpider <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218870130">(Dec 04 2020 at 18:06)</a>:</h4>
<p>I may need to make it unsafe anyways, just in case. But <code>borrow</code>, at least, I can't find a way around so far.</p>



<a name="218870343"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218870343" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218870343">(Dec 04 2020 at 18:08)</a>:</h4>
<p><span class="user-mention silent" data-user-id="319144">Rune Tynan</span> <a href="#narrow/stream/122651-general/topic/Lifetime.20of.20value/near/218825793">said</a>:</p>
<blockquote>
<p>Playground Example: <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=429714bfb40de5056e29290be8892910">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=429714bfb40de5056e29290be8892910</a></p>
</blockquote>
<p>Here (one of) the issue(s) is that <code>Box&lt;dyn Trait …&gt;</code> like in your case is actually a <code>Box&lt;dyn Trait … + 'static&gt;</code>, meaning that your closure must be <code>'static</code>. And the "surprising" part may be that for a given type <code>T</code>, <code>fn(T) -&gt; T</code> is only <code>'static</code> if <code>T</code> is (and more generally, any signature somehow involving some type <code>T</code> will be infected by the maximum lifetime of <code>T</code>)</p>



<a name="218871043"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218871043" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> CraftSpider <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218871043">(Dec 04 2020 at 18:13)</a>:</h4>
<p>Thanks for the explanation of that. Is there, in theory, some way 'around' this issue? Is this 'I can't express a better bound that would be safe' or 'the compiler / the people who wrote it are smarter than me, there is no more specific bound that would be safe'?</p>



<a name="218872773"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218872773" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218872773">(Dec 04 2020 at 18:27)</a>:</h4>
<p>This is my most promising attempt without touching <code>HelperFn</code>, which failed: <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=2548d9e7d700d483da4c493fec346b87">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=2548d9e7d700d483da4c493fec346b87</a> (I have used the <code>fn…</code> function pointer type to remove <code>T0</code> from the function signature).<br>
It kind of makes sense though: given the signatures (mainly that in <code>HelperFn</code>) we are promising that we can handle _any_ lifetime <code>'a</code>, including a lifetime that would span beyond the region of validity of that inner <code>T0</code>.</p>
<hr>
<p>Actually, once that was observed, it sufficed to add a <code>'max</code> bound on the higher-order <code>'a</code>, which requires a helper trait but is doable: <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=eaf7ee1f31733ee5f6c9832fa822e549">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=eaf7ee1f31733ee5f6c9832fa822e549</a></p>



<a name="218910732"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218910732" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> CraftSpider <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218910732">(Dec 05 2020 at 01:24)</a>:</h4>
<p>New question (Not sure whether starting a new thread would be more appropriate):<br>
Assume I have a function <code>cast&lt;T: ...&gt;(mut self) -&gt; Result&lt;T, Error&gt;</code>, and I really want to have the reverse of a <code>'a</code> bound. I want to guarantee T does <em>not</em> outlive <code>'a</code>. Is there any way to do that?</p>



<a name="218922415"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218922415" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218922415">(Dec 05 2020 at 06:57)</a>:</h4>
<p>Usually you would bake it into the type somehow like <code>Guard&lt;'a, T&gt;</code></p>



<a name="218935941"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218935941" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218935941">(Dec 05 2020 at 13:48)</a>:</h4>
<p>Just be careful of <code>Clone</code> / <code>Copy</code>ing capabilities, otherwise you end up <a href="https://docs.rs/libloading/0.6.6/libloading/struct.Symbol.html#impl-Deref">offering an unsound API</a></p>



<a name="218949865"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218949865" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218949865">(Dec 05 2020 at 19:40)</a>:</h4>
<p>Hmm, what do you mean <span class="user-mention" data-user-id="232018">@Daniel Henry-Mantilla</span>?</p>



<a name="218952573"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218952573" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> CraftSpider <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218952573">(Dec 05 2020 at 20:40)</a>:</h4>
<p>The issue is that I specifically need to throw away the <code>T</code>, but it is ensured that no one will safely make a <code>Value&lt;'a&gt;</code> with an <code>'a</code> greater than the life of the <code>T</code> used to make it. So if I could declare a 'does not outlive' bound, the casting should be safe. As I can't, I'm stuck doing as many checks on it as I can and making the function unsafe.<br>
(Yes, I have a lot of weird requirements. It's because I'm trying to do reflection, so most of my types cannot be generic, but have to store type info at runtime. If this turns out to be impossible, fine, but I'm doing it partially because I like the challenge.)</p>



<a name="218958932"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218958932" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218958932">(Dec 05 2020 at 23:29)</a>:</h4>
<p><span class="user-mention" data-user-id="307537">@Camelid</span> assuming a <code>Guard&lt;'a, T&gt;</code> with <code>Deref</code> is used on something that is able to create a <code>T</code> outside the <code>Guard</code> (<em>e.g.</em>, because it is <code>Clone</code>), then you end up with that second instance of <code>T</code> that is no longer tied to<code>'a</code>. This can be very problematic when the <code>T</code> is still a reference type to some value that was only valid for <code>'a</code>.</p>
<p>In the case of libloading, for instance, <code>'a</code> is <code>'lib</code>, the lifetime of the dynamically loaded library (which may be unloaded, deallocating the "<code>'static</code>" data inside), and the common problematic reference type <code>T</code> is <code>fn(...) = &amp;'static code</code>. So, with a <code>Symbol&lt;'lib, fn(...)&gt;</code>, you can simply <code>Copy</code> the inner <code>fn</code> and get something that is no longer <code>'lib</code>-limited, even though the referee, the <code>code</code>, is.</p>



<a name="218959163"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218959163" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218959163">(Dec 05 2020 at 23:35)</a>:</h4>
<p>Can you do that outside of unsafe code?</p>



<a name="218959754"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218959754" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218959754">(Dec 05 2020 at 23:53)</a>:</h4>
<p>Well, you do need the initial <code>unsafe</code> call to load the library and cast the obtained address to a <code>Symbol&lt;'lib, fn(...)&gt;</code>, but assuming you don't get the loading part wrong (<em>i.e.</em>, the symbol does refer to a function with the right signature and ABI), then it's supposed to be a correct / "safe" usage of the library. And at that point, that's where you can, without <code>unsafe</code>, copy the <code>fn()</code> wrappee, drop the library, and (try to) call the <code>fn()</code>. See <a href="https://github.com/nagisa/rust_libloading/issues/13">https://github.com/nagisa/rust_libloading/issues/13</a> .</p>
<p>Sadly the only way to "solve" that would be to remove the <code>Deref</code>, and use some macro to impl an inherent <code>.call()</code> method on <code>Symbol&lt;'lib, fn(...)&gt;</code>, which is deemed to unergonomic. So they're going, it seems, with the approach of saying that that first use of <code>unsafe</code> should also cover a "you shall not copy/clone <code>'static</code> reference types": <a href="https://github.com/nagisa/rust_libloading/issues/46">https://github.com/nagisa/rust_libloading/issues/46</a></p>



<a name="218959956"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Lifetime%20of%20value/near/218959956" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> CraftSpider <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Lifetime.20of.20value.html#218959956">(Dec 05 2020 at 23:59)</a>:</h4>
<p>And while that's not the exact same, it's similar to how my <code>Value&lt;'a&gt;</code> can be consumed to make a <code>T</code>, and if that T would outlive the T used to make it, it's unsound. Meaning <code>cast</code> has to be unsafe, because I can't constrain T to less than <code>'a</code>, only more than.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>